package com.achievement.controller;

import com.achievement.domain.dto.CopyRightDTO;
import com.achievement.domain.param.CopyRightParam;
import com.achievement.domain.vo.CopyRightVO;
import com.achievement.service.ICopyRightService;
import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/achievement/copyRight")
@Api(tags = "成果管理模块---著作权管理")
public class CopyRightController extends BaseController
{
    @Autowired
    private ICopyRightService copyRightService;


    @PreAuthorize("@ss.hasPermi('achievement:copyRight:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页获取著作权列表", notes = "分页获取著作权列表")
    public TableDataInfo list(CopyRightParam param)
    {
        startPage();
        List<CopyRightVO> list = copyRightService.selectByPage(param);
        return getDataTable(list);
    }

    /**
     * 左上方统计 全部 未启动 申请中 已停止
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:list')")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics(CopyRightParam param)
    {
        List<HashMap<String, Integer>> list =copyRightService.listStatistics(param);

        return success(list);
    }

    /**
     * 著作权成果导出
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:export')")
    @Log(title = "著作权成果导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "著作权成果导出", notes = "著作权成果导出")
    public void export(HttpServletResponse response,  @RequestBody Map<String,Object> requestMap)
    {
        CopyRightParam param = JSONObject.parseObject(JSONObject.toJSONString(requestMap), CopyRightParam.class);
        List<String> selectedColumns = (List<String>) requestMap.get("selectedColumns");
        List<CopyRightVO> list = copyRightService.getListByConditions(param);
        ExcelUtil<CopyRightVO> util = new ExcelUtil<>(CopyRightVO.class);
        util.showColumn(selectedColumns.toArray(new String[0]));
        util.exportExcel(response, list, "著作权成果导出数据");
    }

    /**
     * 获取著作权详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取著作权详情", notes = "获取著作权详情")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(copyRightService.getInfoById(id));
    }

    /**
     * 新增著作权
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:add')")
    @Log(title = "新增著作权", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增著作权", notes = "新增著作权")
    public AjaxResult add(@RequestBody CopyRightDTO dto)
    {
        AjaxResult result = copyRightService.add(dto);
        return result;
    }

    /**
     * 修改著作权
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:edit')")
    @Log(title = "修改著作权", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    @ApiOperation(value = "修改著作权", notes = "修改著作权")
    public AjaxResult edit(@RequestBody CopyRightDTO dto)
    {
        return copyRightService.update(dto);
    }

    /**
     * 删除著作权
     */
    @PreAuthorize("@ss.hasPermi('achievement:copyRight:remove')")
    @Log(title = "删除著作权", businessType = BusinessType.DELETE)
	@DeleteMapping("/del/{ids}")
    @ApiOperation(value = "删除著作权", notes = "删除著作权")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(copyRightService.batchRemove(ids));
    }
}
